package com.code.train;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@MapperScan("com.code.train.mapper")
@EnableConfigurationProperties
@EnableTransactionManagement
@SpringBootApplication
@EnableFeignClients("com.code.train.feign")
@EnableCaching
public class TrainBusinessApplication {

	public static void main(String[] args) {

//		SpringApplication.run(TrainApplication.class, args);
		SpringApplication application = new SpringApplication(TrainBusinessApplication.class);
		ConfigurableEnvironment environment = application.run(args).getEnvironment();
		log.info("business模块正常启动");
		log.info("初始地址为: \thttp://localhost:{}" ,environment.getProperty("server.port"));
		//限流
		initFlowRules();
		log.info("已定义限流规则");
	}

	private static void initFlowRules(){
		List<FlowRule> rules = new ArrayList<>();
		FlowRule rule = new FlowRule();
		rule.setResource("doConfirm");
		rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
		// Set limit QPS to 20.
		rule.setCount(1);
		rules.add(rule);
		FlowRuleManager.loadRules(rules);
	}

}
